<!--
@author  tanyuze
@desc    云展馆
@date    2022/12/1
-->
<template>
  <div class="cloudPavilion-index">
    <el-collapse-transition>
      <section v-show="searchshow" class="main-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input
          v-model.trim="listQuery.mainTitle"
          placeholder="输入名称搜索"
          class="search-input"
          @keyup.enter.native="handleFilter"
        />
        <section class="search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
          <el-button type="primary" @click="handleReset">重置</el-button>
          <span class="search-more" @click="searchshow = !searchshow">
            更多筛选条件
            <i class="el-icon-arrow-down" />
          </span>
        </section>
      </section>
    </el-collapse-transition>
    <el-collapse-transition>
      <div v-show="!searchshow" class="main-search separate">
        <div class="search-inner">
          <el-row type="flex">
            <el-col :span="8">
              <div class="search-inner-item">
                <div class="item-label">展馆名称</div>
                <el-input
                  v-model.trim="searchlist.mainTitle"
                  placeholder="请输入展馆名称搜索"
                  class="search-input"
                  @keyup.enter.native="handleFilter"
                />
              </div>
            </el-col>
            <el-col :span="8">
              <div class="search-inner-item">
                <div class="item-label">展馆分类</div>
                <el-cascader
                  v-model.trim="searchlist.newsType"
                  style="width:100%"
                  clearable
                  :props="createClassifyProps"
                  :options="classifyOptions"
                />
              </div>
            </el-col>
          </el-row>
        </div>
        <div class="search-btn">
          <el-button type="primary" @click="handleFilter">搜索</el-button>
          <el-button class="filter-item" type="primary" @click="handleReset">重置</el-button>
          <span class="search-more" @click="searchshow = !searchshow">
            精简筛选条件
            <i class="el-icon-arrow-up" />
          </span>
        </div>
      </div>
    </el-collapse-transition>

    <div class="page-btn">
      <div class="page-title">
        <span>云展馆</span>
      </div>
      <div class="page-btns">
        <el-button style="margin-left: 10px;" @click="handleCreated()">新建</el-button>
      </div>
    </div>

    <!--    <div class="page-tabs">
        <el-tabs v-model="activeTab" @tab-click="handleTabClick">
          <el-tab-pane v-for="(item, index) in dict['DKZYLX']" :key="index" :label="item.dictName" :name="item.dictValue" />
        </el-tabs>
      </div>-->

    <div class="page-main-list">
      <v-table
        :list="list"
        tag="cloud-pavilion"
        :source-arr="sourceArr"
        :selection="false"
        :list-loading="listLoading"
        :filter-column="filterColumn"
      >
        <el-table-column fixed="right" label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="handlePublish(scope.row)">
              {{ scope.row.status === 'GIPSTSTUS01' ? '取消发布' : '发布' }}
            </el-button>
            <el-button type="text" size="mini" @click="handleTop(scope.row)">{{
              scope.row.isTop == '1' ? '取消置顶' : '置顶'
            }}
            </el-button>
            <el-button type="text" size="small" @click="handleDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </v-table>
    </div>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>
<script>
import Pagination from '@/components/Pagination'
import { queryPage, release, deleteById, updateIsTop, fetchList} from '../api'
import elDragDialog from '@/directive/el-drag-dialog'
import vTable from '@/components/Table'

export default {
  name: 'CloudPavilionIndex',
  components: { Pagination, vTable },
  directives: { elDragDialog },
  data() {
    return {
      createClassifyProps: {
        label: 'classifyName',
        value: 'id',
        checkStrictly: true,
        emitPath: false
      },
      classifyOptions: [],
      /* search*/
      searchshow: true,
      total: 0,
      listQuery: {
        page: 1,
        limit: 10,
        mainTitle: ''
      },
      searchlist: {
        mainTitle: '',
        newsType: []
      },
      search: {
        title: ''
      },

      /* table*/
      list: [],
      listLoading: false,
      sourceArr: ['coverImgIdUrl']

      /* user-defined*/

    }
  },
  computed: {
    /* tag() {
      return this.tagList[this.activeTab]
    }*/
  },
  watch: {
    /* activeTab() {
      this.myTime = new Date().getTime()
    }*/
  },
  created() {
    this.getClassifyOption()
    this.getList()
  },
  methods: {
    /**
     * 获取分类
     */
    getClassifyOption() {
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      return new Promise((resolve, reject) => {
        fetchList({ classifyType: 'XWFL_YZG' })
          .then(({ data }) => {
            loading.close()
            this.classifyOptions = data.data || []
            resolve()
          })
          .catch((error) => {
            loading.close()
            reject(error)
          })
      })
    },
    filterColumn(scope, item) {
      return scope.row[item]
    },
    handleCreated() {
      this.$router.push({ path: this.$filterPath('manage'), query: { type: 'c' } })
    },
    handleUpdate(row) {
      this.$router.push({ path: this.$filterPath('manage'), query: { type: 'u', i: row.id } })
    },

    /**
     * 发布/取消发布
     */
    handlePublish(row) {
      this.$confirm(row.status === 'GIPSTSTUS01' ? '确认取消发布？' : '确认发布？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        release({ id: row.id, status: row.status === 'GIPSTSTUS01' ? 'GIPSTSTUS02' : 'GIPSTSTUS01' }).then(res => {
          if (res.data.code === '200') {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.getList()
          }
        })
      }).catch(() => {

      })
    },
    /**
     * 置顶/取消置顶
     */
    handleTop(row) {
      this.$confirm(row.isTop === '1' ? '确认取消置顶？' : '确认置顶？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateIsTop({ id: row.id, isTop: row.isTop === '1' ? '2' : '1' }).then(res => {
          if (res.data.code === '200') {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.getList()
          }
        })
      }).catch(() => {

      })
    },
    /**
     * 删除
     */
    handleDel(row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({
            lock: true,
            text: '请稍后',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.3)'
          })
          return new Promise((resolve, reject) => {
            deleteById({
              id: row.id
            })
              .then(() => {
                loading.close()
                this.$message({
                  title: '成功',
                  message: '删除成功',
                  type: 'success',
                  duration: 2000
                })
                this.getList()
              })
              .catch((error) => {
                loading.close()
                reject(error)
              })
          })
        })
        .catch(() => {
        })
    },

    /**
     * 获取列表
     */
    getList(obj) {
      this.listLoading = true
      var query = {
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit
      }
      if (this.searchShow) {
        query['mainTitle'] = this.listQuery.mainTitle
      } else {
        query['mainTitle'] = this.searchlist.mainTitle
        query['newsType'] = this.searchlist.newsType.length > 0 ? this.searchlist.newsType : ''
      }
      return new Promise((resolve, reject) => {
        queryPage(query)
          .then((response) => {
            this.listLoading = false
            let dataList = []
            if (response.data.data.rows && response.data.data.rows.length > 0) {
              dataList = response.data.data.rows.map((item, index) => {
                const obj = item
                if (item.bigCoverImgIdUrl) {
                  obj['coverImgIdUrl'] = item.bigCoverImgIdUrl.split(',')[0]
                }
                return obj
              })
            }
            this.list = dataList
            this.total = response.data.data.total
          })
          .catch((error) => {
            this.listLoading = false
            reject(error)
          })
      })
    },

    /**
     *搜索
     */
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    /**
     *重置
     */
    handleReset() {
      this.listQuery.mainTitle = ''
      this.listQuery.page = 1
      this.searchlist.newsType = ''
      this.getList()
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss">
.cloudPavilion-index {
  padding: 10px;

  .main-search {
    background-color: #fff;
    padding: 0 10px;

    .svg-container {
      padding: 0 5px 0 10px;
      color: #bfced6;
      vertical-align: middle;
      display: inline-block;
    }

    .search-input {
      width: 60%;
      position: relative;

      &:after {
        content: '';
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }

      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;

        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }

    .search-btn {
      display: inline-block;
      float: right;

      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }

      .search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
        margin-left: 10px;
      }
    }

    &.merge {
      height: 50px;
      line-height: 50px;
    }

    &.separate {
      .search-inner {
        padding-top: 10px;

        .search-inner-item {
          .item-label {
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            color: #a6a6a6;
            font-weight: 400;
          }

          .el-input {
            position: relative;

            &.el-date-editor input {
              padding-left: 30px;
            }

            .el-input__inner {
              border: none;
              padding: 0;
            }

            &:after {
              content: '';
              height: 1px;
              background-color: #e2e3e8;
              position: absolute;
              left: 0;
              right: 0;
              bottom: 0;
            }
          }
        }
      }

      .search-btn {
        text-align: center;
        display: block;
        float: none;
        height: 56px;
        line-height: 56px;
      }
    }
  }

  .page-btn {
    display: flex;
    height: 46px;
    line-height: 46px;
    width: 100%;

    .page-title {
      flex: 1;
      font-size: 16px;
      font-weight: bold;
      color: rgba(51, 51, 51, 1);
    }
  }

  .page-tabs {
    width: 100%;
    margin: 10px 0;
    padding: 0 10px;
    background-color: #fff;
    border-radius: 4px;

    .el-tabs__nav-next {
      line-height: 40px;
    }

    .el-tabs__nav-wrap::after {
      display: none;
    }

    .el-tabs__header {
      margin: 0;
    }
  }

  .page-main-list {
    .el-image {
      width: 100%;
      height: auto;
      max-height: 140px;
    }
  }

  .pagination-container {
    background: none;
  }

}
</style>
